//
//  WLLStatusViewModel.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WLLStatusViewModel: NSObject {
    //创建一个对象
    var statusModel :WLLStatusModel?
    
    /// 微博等级图片
    var mbrankImage: UIImage?
    
    /// 微博认证图片
    var verifiedImage: UIImage?
    
    /// 微博来源
    var sinaSourceStr: String?
    /// 转发
    var repostsCountStr: String?
    /// 评论
    var commentsCountStr: String?
    /// 赞
    var attitudesCountStr: String?
    /// 微博时间(计算型属性)
    var sinaTimeStr:String?{
        return NSDate.getSinaDate(statusModel?.created_at ?? "").sinaDateDescription
    }
    
    
    init(statusModel :WLLStatusModel){
        super.init()
        self.statusModel = statusModel
        //MARK: - 存储性属性
        //自创微博中
        mbrankImage = dealMbrankImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealVerifiedImage(statusModel.user?.verified ?? -1)
        
        //微博来源
        sinaSourceStr = dealSinaSourceStr(statusModel.source ?? "")
        
        //底部视图的转发,评论,赞
        //TODO:为什么在这里是可选值
        repostsCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "转发")
        /// 评论
        commentsCountStr = getSinaCountStr(statusModel.comments_count ?? 0, title: "评论")
        /// 赞
        attitudesCountStr = getSinaCountStr(statusModel.attitudes_count ?? 0, title: "zan")
        
    }
    
    
}

/*******************************抽取到 ViewModel 中**********************************************/
//处理底部视图,转发,评论,赞
extension WLLStatusViewModel{
    /*
     - 底部视图的转发 评论 赞  业务逻辑
     - 如果 count <= 0  直接显示 转发 评论 赞
     - 如果 count > 0 && count < 10000  是多少显示多少  示例  8888  8888
     - 如果 count >= 10000   示例 12000   显示  1.2万
     - 如果 count%10000 == 0   示例 20000  显示  2万
     */
    // 处理底部视图 转发 评论 赞
    func getSinaCountStr (count:Int,title:String) -> String {
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000 {
            return "\(count)"
        }else{
            let result = CGFloat(count/1000)/10
            var resultStr = "\(result)万"
            //判断是否包含".0"
            if resultStr.containsString(".0") {
                //字符串替换
                resultStr  = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
        }
    }
}
//处理微博来源
extension WLLStatusViewModel{
    func dealSinaSourceStr(source:String) -> String {
        printLog("s 帅帅的乐乐")
        // 判断字符串是否包含"> 特殊字符
        //注意这里有空格会影响结果!!
        if source.containsString("\">") {
            //获取 range
            let startIndex = source.rangeOfString("\">")?.endIndex
            let endIndex = source.rangeOfString("</")?.startIndex
            //字符串截取
            let result = source.substringWithRange(startIndex!..<endIndex!)
            return "来自\(result)"
        }
        
        
        
        //如果为空字符串
        return "在那桃花盛开的地方"
        
    }
}
//WLLStatusOriginalView中的代码提取
extension WLLStatusViewModel{
    /**
     认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
     */
    func dealVerifiedImage(verified:Int) -> UIImage? {
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    
    /**
     会员等级 1-6
     */
    func dealMbrankImage(mbrank:Int) -> UIImage? {
        if mbrank>0 && mbrank<7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return UIImage(named: "common_icon_membership")
        
    }
    
    
}



